//! @Author: DengLibin
//! @Date: Create in 2023-11-02 17:03:19
//! @Description:
//!

use eframe::egui;

use crate::app_global::load_global_font;

#[allow(dead_code)]
pub fn hello() -> Result<(), eframe::Error> {
    // env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).

    let options = eframe::NativeOptions {
        initial_window_size: Some(egui::vec2(640.0, 480.0)),
        ..Default::default()
    };
    eframe::run_native(
        "hello world",
        options,
        Box::new(|cc| {
            // This gives us image support: 支持图片
            egui_extras::install_image_loaders(&cc.egui_ctx);
            //加载全局字体
            load_global_font(&cc.egui_ctx);

            Box::<MyApp>::default()
        }),
    )
}

struct MyApp {
    name: String,
    age: u32,
}

/// @Author: DengLibin
/// @Date: Create in 2023/11/6 10:34
/// @Description:
impl Default for MyApp {
    fn default() -> Self {
        Self {
            name: "Arthur".to_owned(),
            age: 42
        }
    }
}

impl eframe::App for MyApp {
    /// @Author: DengLibin
    /// @Date: Create in 2023-11-03 08:47:00
    /// @Description: update 函数 会循环调用
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        

        egui::CentralPanel::default().show(ctx, |ui| {
            //添加布局
            ui.heading("第一个示例"); //头部
                                      //水平布局
            ui.horizontal(|ui| {
                let name_label = ui.label("您的名字: "); //label
                                                         //单行文本框
                ui.text_edit_singleline(&mut self.name)
                    .labelled_by(name_label.id);
            });

            //添加滑块
            ui.add(egui::Slider::new(&mut self.age, 0..=120).text("年龄"));
            if ui.button("增加一年").clicked() {
                self.age += 1; //年龄增加
            }
            //添加lable
            ui.label(format!("Hello '{}', 年龄 {}", self.name, self.age));

            //图片
            ui.image(egui::include_image!("../assets/ferris.png"));
        });
    }
}
